//
//  CAIStorageManager.m
//  Runner
//
//  Created by simon on 2021/8/31.
//

#import "CAIStorageManager.h"

@interface CAIStorageManager ()

@property (nonatomic, strong) CAIFileCache *fileCache;

@end


@implementation CAIStorageManager

+ (instancetype)sharedInstance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithName:@""];
    });
    return instance;
}

- (instancetype)initWithName:(nullable NSString *)name
{
    if (self = [super init]) {
        _fileCache = [[CAIFileCache alloc] initWithName:name];
    }
    return self;
}

- (instancetype)initWithName:(nullable NSString *)name rootPath:(NSString *)rootPath
{
    if (self = [super init]) {
        _fileCache = [[CAIFileCache alloc] initWithName:name rootPath:rootPath];
    }
    return self;
}

- (void)setObject:(id)object forKey:(NSString *)key{
    [self.fileCache setObject:object forKey:key];
}

- (void)setEncodeCryptObject:(id)object
                      forKey:(NSString *)key
                       error:(NSError * __autoreleasing *)error
{
    [self.fileCache setEncodeCryptObject:object forKey:key error:error];
}


- (void)archiveObject:(id <NSCoding>)object forKey:(NSString *)key{
    [self.fileCache archiveObject:object forKey:key];
}

- (void)archiveEncodeCryptObject:(id <NSSecureCoding>)object
                          forKey:(NSString *)key
                           error:(NSError * __autoreleasing *)error{
    [self.fileCache archiveEncodeCryptObject:object forKey:key error:error];
}


- (id)objectForKey:(NSString *)key{
    return[self.fileCache objectForKey:key];
}


- (id)objectDecodeCryptForKey:(NSString *)key
                expectedClass:(Class)cls
                        error:(NSError * __autoreleasing *)error
{
    return [self.fileCache objectDecodeCryptForKey:key expectedClass:cls error:error];
}

- (void)removeObjectForKey:(NSString *)key{
    [self.fileCache removeObjectForKey:key];
}

- (BOOL)createTable:(NSString *)tableName
                     model:(nullable id)model
              error:(NSError * __autoreleasing *)error{
    return [[CAIFMDB sharedInstance]createGeneralTable:tableName model:model error:error];
}

- (BOOL)setTable:(NSString *)tableName
              model:(id)model
              error:(NSError * __autoreleasing *)error{
    
    return [[CAIFMDB sharedInstance]setTable:tableName model:model error:error];
}

- (BOOL)setEncodeWithTable:(NSString *)tableName
                            model:(id)model
                         error:(NSError * __autoreleasing *)error{
    return [[CAIFMDB sharedInstance]setEncodeTable:tableName model:model error:error];
}


- (id)fetchTable:(NSString *)tableName modelClass:(Class)cls{
    return  [[[CAIFMDB sharedInstance]fetchTable:tableName modelClass:cls] firstObject];
    
}

- (id)fetchDecodeWithTable:(NSString *)tableName modelClass:(Class)cls{
    return [[CAIFMDB sharedInstance]fetchDecodeTable:tableName modelClass:cls];
}

- (BOOL)dropTable:(NSString *)tableName{
    return  [[CAIFMDB sharedInstance]dropTable:tableName];
}

@end
